/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package control;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.DAO.EstudianteDao;
import modelo.Estudiante;

/**
 *
 * @author Carlos Jose Mendoza
 */
public class ControlEstudiante extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        
        //try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
        /*
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ControlEstudiante</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ControlEstudiante at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
        */
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
                   response.setContentType("text/html;charset=UTF-8");

         // PrintWriter oute = response.getWriter();
          //try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            /*  
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ControlEstudiante</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ControlEstudiante at " + a + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
            
            */ 
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
      //  if(request.getServletPath().equals("/MostrarEstudiante")){
           // response.setContentType("text/html");
           EstudianteDao es = new EstudianteDao();         
          
        //} else 
           Estudiante e = new Estudiante();
            if(request.getServletPath().equals("/RegistrarEstudiante")) {
            e.setNombre(request.getParameter("nombre"));
            e.setPrimerApellido(request.getParameter("apellido1"));
            e.setSegundoApellido(request.getParameter("apellido2"));
            e.setSexo(request.getParameter("sexo")); 
             String vecChar[] = new String[12];
            String fech = request.getParameter("fechanacimiento");
            Character a = null;
            for(int i=0;i<fech.length();i++){
                a = fech.charAt(i);
                    vecChar[i] = a+"";
            }
            String fechaTotal =vecChar[8] + vecChar[9] + vecChar[4]  + vecChar[5] + vecChar[6] + vecChar[7] + vecChar[0] + vecChar[1] +vecChar[2] + vecChar[3];
            e.setFechaNacimiento(fechaTotal);
            e.setNumeroIdentificacion(request.getParameter("id"));
            e.setCalle(request.getParameter("calle")); 
            e.setBarrio(request.getParameter("barrio")); 
            e.setManzana(request.getParameter("manzana")); 
            e.setCiudad(request.getParameter("ciudad")); 
            e.setNumeroTelefono(request.getParameter("telefono"));
            e.setCorreo(request.getParameter("email"));
            e.setSemestreActual(request.getParameter("semestreActual"));
            
           boolean estado = es.registrar(e);
           response.setContentType("text/html");
           PrintWriter out = response.getWriter();
           if(estado==false){
               response.sendRedirect("index.html");
           } else{
               String codigo = es.codigoUsuario(e.getNumeroIdentificacion(), "codigoAspirante", "aspirantes", "numeroIdentificacionAspirante");
               request.getSession().setAttribute("usuario", e.getNumeroIdentificacion());
               request.getSession().setAttribute("password", codigo);
            response.sendRedirect("/proyectoHojaDeVida/vistas/pgConfirmacion/confirmacion_registro.jsp");
           }
            } else { if(request.getServletPath().equals("/ModificarEstudiante")) {
                
            e.setNombre(request.getParameter("nombre"));
            e.setPrimerApellido(request.getParameter("apellido1"));
            e.setSegundoApellido(request.getParameter("apellido2"));
            e.setFechaNacimiento(request.getParameter("fechanacimiento"));
            e.setSexo(request.getParameter("sexo"));
            e.setNumeroIdentificacion(request.getParameter("id"));
            e.setCalle(request.getParameter("calle"));
            e.setBarrio(request.getParameter("barrio"));
            e.setManzana(request.getParameter("manzana"));
            e.setCiudad(request.getParameter("ciudad"));
            e.setNumeroTelefono(request.getParameter("telefono"));
            e.setCorreo(request.getParameter("email"));
            e.setSemestreActual(request.getParameter("semestreActual"));
            
           boolean estado = es.modificar(e);
           if(estado==true) {
                String codigo = es.codigoUsuario(e.getNumeroIdentificacion(), "codigoAspirante", "aspirantes", "numeroIdentificacionAspirante");
               request.getSession().setAttribute("usuario", e.getNumeroIdentificacion());
               request.getSession().setAttribute("password", codigo);
             response.sendRedirect("/proyectoHojaDeVida/vistas/enlaces/Modificar_Estudiante.jsp");
           } else {
               
           }
            }
            
            }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
